package _binary_search;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 1146. 快照数组
 */
public class No1146 {
    private final HashMap<Integer, TreeMap<Integer, Integer>> DB;
    private int snapId = 0;

    public No1146(int length) {
        DB = new HashMap<>(length);
    }

    public void set(int index, int val) {
        TreeMap<Integer, Integer> treeMap = DB.getOrDefault(index, new TreeMap<>());
        treeMap.put(snapId, val);
        DB.put(index, treeMap);
    }

    public int snap() {
        return snapId++;
    }

    public int get(int index, int snap_id) {
        TreeMap<Integer, Integer> treeMap = DB.get(index);
        if (treeMap == null) return 0;
        Map.Entry<Integer, Integer> entry = treeMap.floorEntry(snap_id);
        if (entry == null) return 0;
        return entry.getValue();
    }
}
